package com.insight.modules.message.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.insight.common.api.vo.Result;
import com.insight.common.aspect.annotation.AutoLog;
import com.insight.common.system.base.controller.JeecgController;
import com.insight.common.system.query.QueryGenerator;
import com.insight.modules.message.entity.SysMessage;
import com.insight.modules.message.service.ISysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 消息
 * @author: jeecg-boot
 * @date: 2019-04-09
 * @version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/sys/message/sysMessage")
public class SysMessageController extends JeecgController<SysMessage, ISysMessageService> {

	@Autowired
	private ISysMessageService sysMessageService;

	/**
	 * 分页列表查询
	 * @param sysMessage 查询条件封装对象
	 * @param pageNo 当前页码，默认1
	 * @param pageSize 每页条数，默认10
	 * @param req HTTP请求对象，用于获取请求参数
	 * @return 分页结果封装的Result对象
	 */
	@AutoLog(value = "消息管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SysMessage sysMessage,
								   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysMessage> queryWrapper = QueryGenerator.initQueryWrapper(sysMessage, req.getParameterMap());
		Page<SysMessage> page = new Page<>(pageNo, pageSize);
		IPage<SysMessage> pageList = sysMessageService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 添加消息
	 * @param sysMessage 消息实体
	 * @return 添加结果
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysMessage sysMessage) {
		sysMessageService.save(sysMessage);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑消息
	 * @param sysMessage 消息实体
	 * @return 编辑结果
	 */
	@AutoLog(value = "消息管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SysMessage sysMessage) {
		sysMessageService.updateById(sysMessage);
		return Result.ok("修改成功!");
	}

	/**
	 * 通过ID删除消息
	 * @param id 消息ID
	 * @return 删除结果
	 */
	@AutoLog(value = "消息管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		sysMessageService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除消息
	 * @param ids 多个消息ID，逗号分隔
	 * @return 删除结果
	 */
	@AutoLog(value = "消息管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.sysMessageService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过ID查询消息
	 * @param id 消息ID
	 * @return 查询结果
	 */
	@AutoLog(value = "消息管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		SysMessage sysMessage = sysMessageService.getById(id);
		return Result.ok(sysMessage);
	}

	/**
	 * 导出消息为Excel文件
	 * @param request 请求对象
	 * @param sysMessage 查询条件
	 * @return 导出视图
	 */
	@AutoLog(value = "消息管理-导出excel")
	@GetMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, SysMessage sysMessage) {
		return super.exportXls(request, sysMessage, SysMessage.class, "推送消息模板");
	}

	/**
	 * Excel导入消息数据
	 * @param request 请求对象
	 * @param response 响应对象
	 * @return 导入结果
	 */
	@AutoLog(value = "消息管理-excel导入")
	@PostMapping(value = "/importExcel")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, SysMessage.class);
	}
}

